/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */
package loginbean;

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 	protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		
		// TODO Auto-generated method stub
		
		Connection connection=null;
		ResultSet rs;
		//response.setContentType("text/html");
		Connection conn = null;
		DBManager db = new DBManager();
		
		String tenantName = req.getParameter("tenantName");
		String tenantEmail = req.getParameter("tenantEmail");
		String tenantPassword = req.getParameter("tenantPassword");
		 
		try
		{
		  System.out.println("before classforname");
		  conn = db.getDatabaseConnection();
		  System.out.println("after classforname connection");
		  HttpSession session = req.getSession(true);
		  
		String sql = "insert into mydb.tenanttable (TenantName,TenantPassword,TenantEmail,RegisterDate)" +
		" VALUES(?,?,?,CURRENT_TIMESTAMP)";
		
		PreparedStatement pst = conn.prepareStatement(sql);
		System.out.println(sql);
		pst.setString(1, tenantName);
		pst.setString(2, tenantPassword);
		pst.setString(3, tenantEmail);
		int numRowsChanged = pst.executeUpdate();
		pst.close();
		
		if(numRowsChanged>0)
		  {
			//to get tenant ID :(
			Statement st=null;
			sql="select max(tenantid) from tenanttable";
			System.out.println(sql);
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next())
			{
				session.setAttribute("tenantid",rs.getInt(1));
			}
		  session.setAttribute("username",tenantName);	
		  response.sendRedirect("welcome.jsp");
		  }
		  else
		  {
		  response.sendRedirect("error.jsp");
		  }
		
		}
		catch(ClassNotFoundException e){
		System.out.println("Couldn't load database driver: " + e.getMessage());
		}
		catch(SQLException e){
		System.out.println("SQLException caught: " + e.getMessage());
		}
		catch (Exception e){
		System.out.println(e);
		}
		finally {
		if (connection != null)
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}


}
